<template>
  <view>
    <button class="list-item" hover-class="hover-active" open-type="chooseAvatar" @chooseavatar="onChooseAvatar" v-if="item.value == 'avatar'">
      <view class="l-label">{{item.label}}</view>
      <view class="l-text">
        <image class="l-img" :src="dataForm.pic || defaultImg"></image>
        <view style="width:40rpx;height:40rpx;margin-left:10rpx">
          <image src="@/subPages/static/arrow-right.svg" mode="scaleToFill" style="width: 40rpx; height: 40rpx" />
        </view>
      </view>
    </button>
    <view class="list-item" hover-class="hover-active" @click="toUrl(item.value)" v-else>
      <view class="l-label">{{item.label}}</view>
      <view class="l-text">
        <view v-if="dataForm[item.value]">{{dataForm[item.value]}}</view>
        <view class="l-e-text" v-else>请填写</view>
        <view style="width:40rpx;height:40rpx;margin-left:10rpx">
          <image src="@/subPages/static/arrow-right.svg" mode="scaleToFill" style="width: 40rpx; height: 40rpx" />
        </view>
      </view>
    </view>

    <view class="add-tips" @click="toUrl('mate_region')" v-if="item.value == 'mate_region' && addTotal">
      默认现居地，点击可再新增<text style="color:#FF5C55;font-weight:bold">{{addTotal}}</text>个地区，将会为您推荐所有地区的相亲对象！
    </view>
  </view>
</template>

<script setup>
import avatarImg from "@/static/common/avatar.svg";
import uploadFn from "@/utils/upload";
import { setItem } from "@/utils/storage";
const updateUserInfo = inject("updateUserInfo", () => {});
const emits = defineEmits(["openPopup"]);
const props = defineProps({
  item: {
    type: Object,
    default: () => ({}),
  },
  dataForm: {
    type: Object,
    default: () => ({}),
  },
});

const defaultImg = ref(avatarImg);

const addTotal = computed(() => {
  if (props.dataForm.mate_region) {
    const arr = props.dataForm.mate_region.split(",");
    if (arr.length > 3) {
      return 0;
    } else {
      return 3 - arr.length;
    }
  }

  return 2;
});
const toUrl = (t) => {
  
  if (t == "avatar") return;
  const types = ["surname", "phone", "occupation"];
  const data = { };

  if (types.includes(t)) {
    data[t] = props.dataForm[t]
    setItem("editUser", data);
    uni.navigateTo({ url: "/subPages/complete/other?type=" + t });
    return;
  }
  if (t == "mate_region") {
    data.mate_address = props.dataForm.mate_address
    setItem("editUser", data);
    uni.navigateTo({ url: "/subPages/complete/region" });
    return;
  }

  emits("openPopup", t);
};

// 选择头像
const onChooseAvatar = async (e) => {
  const { avatarUrl } = e.detail;
  const { img } = await uploadFn(avatarUrl, "pic");
  props.dataForm.pic = img;
  updateUserInfo("", "pic");
};
</script>

<style lang=scss scoped>
@import "@/styles/global.scss";
.list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 40rpx 0;
  .l-img {
    width: 60rpx;
    height: 60rpx;
    border-radius: 50%;
  }

  .l-label {
    font-weight: 500;
    font-size: 32rpx;
    color: #000;
  }
  .l-text {
    display: flex;
    align-items: center;
    font-weight: bold;
    font-size: 32rpx;
    color: #000;
  }
  .l-e-text {
    color: #ff5c55;
  }
}

.avatar-wrapper {
  background: transparent;
  border: none;
  width: 100%;
  padding: 0;
  outline: none;
  border-color: transparent;
}
.add-tips {
  padding-bottom: 30rpx;
  font-size: 32rpx;
  color: #999;
  line-height: 40rpx;
}
</style>